package com.thundersoft.beautifulmusic.ui;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RemoteViews;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.thundersoft.beautifulmusic.R;
import com.thundersoft.beautifulmusic.adapter.SongAdapter;
import com.thundersoft.beautifulmusic.entity.Song;
import com.thundersoft.beautifulmusic.other.SongService;
import com.thundersoft.beautifulmusic.util.Constants;
import com.thundersoft.beautifulmusic.util.LogUtil;
import com.thundersoft.beautifulmusic.util.SongUtil;

import java.util.List;

public class LocalFragment extends Fragment {
    private List<Song> songs;
    private ImageView mPlay;
    private ImageView mCover;
    private TextView mName;
    private RecyclerView mRecyclerView;
    private GestureDetector mGestureDetector;
    private MainActivity mMainActivity;

    public static final String TAG = "LocalFragment";


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        LogUtil.d(TAG, "onAttach");
        mMainActivity = (MainActivity) context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: ");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.fragment_local, container, false);
        // get view
        mCover = linearLayout.findViewById(R.id.song_cover_bottom);
        mName = linearLayout.findViewById(R.id.song_name_bottom);
        mPlay = linearLayout.findViewById(R.id.play_stop_bottom);
        RelativeLayout layout = linearLayout.findViewById(R.id.main_bottom);

        mRecyclerView = linearLayout.findViewById(R.id.song_recycler);
        mRecyclerView.addItemDecoration(new DividerItemDecoration(mMainActivity, LinearLayoutManager.VERTICAL));
        LinearLayoutManager manager = new LinearLayoutManager(mMainActivity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
        songs = SongUtil.getSong();
        SongAdapter adapter = new SongAdapter(mMainActivity, songs);
        mRecyclerView.setAdapter(adapter);

        mPlay.setOnClickListener(v -> {
            Intent intent = new Intent("play");
            intent.putExtra("song", songs.get(0));
            mMainActivity.startService(intent);
        });

        layout.setOnClickListener(v -> {
            Intent intent = new Intent(mMainActivity, SongDetailsActivity.class);
            intent.putExtra("song", songs.get(0));
            startActivity(intent);
        });

        // set click events for recyclerview
        mRecyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
            @Override
            public boolean onInterceptTouchEvent(@NonNull RecyclerView rv, @NonNull MotionEvent e) {
                return mGestureDetector.onTouchEvent(e);
            }
        });
        mGestureDetector = new GestureDetector(mMainActivity, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                View child = mRecyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null) {
                    Log.d(TAG, "onSingleTapUp: play");
                    int position = mRecyclerView.getChildLayoutPosition(child);
                    Song song = songs.get(position);
                    Intent intent = new Intent(mMainActivity, SongService.class);
                    intent.putExtra("instruction", Constants.PlayerInstruction.CHANGE);
                    intent.putExtra("song", song);
                    mMainActivity.startService(intent);
                }
                return super.onSingleTapUp(e);
            }
        });
        LogUtil.d(TAG, "onCreateView");
        return linearLayout;
    }

    @Override
    public void onResume() {
        super.onResume();
        LogUtil.d(TAG, "onResume");
    }
}
